package com.imageim.yimirror.base;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Keep;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.imageim.yimirror.ui.activity.BannerActivity;
import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixApplication;
import com.taobao.sophix.SophixEntry;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;

/**
 * Sophix入口类，专门用于初始化Sophix，不应包含任何业务逻辑。
 * 此类必须继承自SophixApplication，onCreate方法不需要实现。
 * 此类不应与项目中的其他类有任何互相调用的逻辑，必须完全做到隔离。
 * AndroidManifest中设置application为此类，而SophixEntry中设为原先Application类。
 * 注意原先Application里不需要再重复初始化Sophix，并且需要避免混淆原先Application类。
 * 如有其它自定义改造，请咨询官方后妥善处理。
 */
public class SophixStubApplication extends SophixApplication {
    private final String TAG = "SophixStubApplication";

    // 此处SophixEntry应指定真正的Application，并且保证RealApplicationStub类名不被混淆。
    @Keep
    @SophixEntry(BaseApplication.class)
    static class RealApplicationStub {
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //         如果需要使用MultiDex，需要在此处调用。
        MultiDex.install(this);
        initSophix();
    }

    private void initSophix() {
        String appVersion = "0.0.0";
        try {
            appVersion = this.getPackageManager()
                    .getPackageInfo(this.getPackageName(), 0)
                    .versionName;
        } catch (Exception e) {
        }
        final SophixManager instance = SophixManager.getInstance();
        instance.setContext(this)
                .setAppVersion(appVersion)
                .setSecretMetaData("25639814", "bf2fe7a4f5edc2633ab5d2e19315b7c2", "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCR4ylsKCfLBOQhep1Y6KwltV02HLuJJ909zcgNKQNHtCfksgcMazPtSclsFU1XkdMRfnP5RstOC85F5D3PyLBKqZl0zAkYHw7iz8DGfGF8GTP9zwU3yUBswM64zHmaQF269FB7cMNvbJzlNPJ9+Q+IA3mcX0ALuzHUj9rZi9Ph6zUwo2WAhuzfkEei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2MQm7+6D+fap4trfYePh+5/InxtOjXPfmzr1ccdbP6qgksNcuuks+4b6WPHYUocauJCqq2Ap1mnCFwkOZjHWLM1k3raEIaa7E9+O7vOzbwkCgYBWvSPOHYhMw8xzN07tHBBSQEmThc2uaFvw4BPfSTkGBzq9Ql3QNmQhCK/HplY8U5jTWTsdUaKaaBpRoJY4/KpHe2+mKpnC7OSU/AVhDwXNAejxqRD98RgqB6jsQSIrLzVzeRoQ0y0seIOtTWbajBM9nf379kXzLwadOVvY61Bi7Q==")
                //调试模式下可以为true 不进行补丁签名校验  正式发布该参数必须为false, false会对补丁做签名校验
                .setEnableDebug(false)
                .setEnableFullLog()
                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    @Override
                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
                        if (code == PatchStatus.CODE_LOAD_SUCCESS) {
                            Log.i(TAG, "sophix load patch success!");
                        } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
                            //预加载阶段, 需要重启 如果需要在后台重启，建议此处用SharePreference保存状态。
                            Log.i(TAG, "sophix preload patch success. restart app to make effect.");
                            restartApp();
                        } else if (code == PatchStatus.CODE_LOAD_FAIL) {
                            // 内部引擎异常, 推荐此时清空本地补丁, 防止失败补丁重复加载
                            // SophixManager.getInstance().cleanPatches();
                            Log.i(TAG, PatchStatus.CODE_LOAD_FAIL + "");
                        } else {
                            // 其它错误信息, 查看PatchStatus类说明
                            Log.i(TAG, code + "----" + info);
                        }
                    }
                }).initialize();
    }


    private void restartApp() {
        Intent intent = new Intent(this, BannerActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
        SophixManager.getInstance().killProcessSafely();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        SophixManager.getInstance().queryAndLoadNewPatch();
    }
}